<html>
<head>
<title>Dojo Testing Script</title>
<script type="text/javascript" src="js/dojo/dojo.js"></script>
<script type="text/javascript" src="js/JSONGenerator.js"></script>
<script type="text/javascript">
    var eventCache = {};
    var venueId ;

</script>

<script type="text/javascript">
dojo.require("dojo.event.*");
dojo.require("dojo.widget.*");
dojo.require("dojo.widget.Button");
dojo.require("dojo.io.XhrIframeProxy");

var json;
document.domain = "upcoming.org";

function onFetchedResult(result) {
    alert(result.innerHTML);
    json = createJSONFromXmlString(result.innerHTML, "rsp");
    alert(json.rsp);
}

function getEvents(xml) {
    var mimeType = "text/xml";
    var xmlDom = dojo.dom.createDocumentFromText(xml, mimeType);
    var events = xmlDom.getElementsByTagName("event");
    var event;
    var listOfEvents = [];

    for (var i = 0; i < events.length; i++) {
        event = events[i];
        listOfEvents.push(makeEvent(event));
    }
    return listOfEvents;
}

function makeEvent(event) {
    var _event = new Object();
    for (var i = 0; i < event.attributes.length; i++) {
        var attribute = event.attributes[i];
        _event[attribute.nodeName] = attribute.nodeValue;
    }
    return _event;
}

function getVenueDetails(data) {
    var mimeType = "text/xml";
    var xmlDom = dojo.dom.createDocumentFromText(data, mimeType);
	
    var _venue = xmlDom.getElementsByTagName("venue")[0];
	
    var venue = new Object();
    for(var i = 0;i < _venue.attributes.length;i++){
        var attribute = _venue.attributes[i];
        venue[attribute.nodeName] = attribute.nodeValue;
    }
    return venue;
}


function blah(data) {
    var events = getEvents(data);
    var event_names = document.getElementById('event_names');
    for (i in events) {
        var event = events[i];
        var option = document.createElement("OPTION");
        event_names.options.add(option);
        //alert(eventCache);
        eventCache[event["id"]] = event;
        option.innerText = event["name"];
        option.value = event["id"];
        //alert(option.value);
    }
    //var o = xml2json(data,true);
    //alert(o);
    //document.getElementById('result_container').innerText = data;
    var _iframe = document.getElementById("id");
    document.body.removeChild(_iframe);
}

function helloPressed() {
    var _iframe = document.createElement('iframe');
    document.body.appendChild(_iframe);
    _iframe.src = "iframe_container.html";
    _iframe.id = "id";
    _iframe.style.display = "none";

}


function venueDetails(data) {
    var venue = getVenueDetails(data);
    var venue_details = document.getElementById("venue_details");
    venue_details.innerText = "Venue \n\t" + venue["name"] + "\n\t" + venue["address"] + "\n\n\t" + "phone \n\t" + venue["phone"];
    var _iframe = document.getElementById("id");
    document.body.removeChild(_iframe);
}

function fetchVenueDetails(id) {
    var _iframe = document.createElement('iframe');
    document.body.appendChild(_iframe);
    venueId = id;
    _iframe.src = "venue_iframe_container.html";
    _iframe.id = "id";
    _iframe.style.display = "none";
}

function showNameForEvent(id) {
    var description = document.getElementById('description');
    description.innerText = eventCache[id]["description"];
    var start_date = document.getElementById('start_date');
    start_date.innerText = "Start Date \n\t" + eventCache[id]['start_date']
    var end_date = document.getElementById('end_date');
    end_date.innerText = "End Date \n\t" + eventCache[id]['end_date']
    var start_time = document.getElementById('start_time');
    start_time.innerText = "Start Time \n\t" + eventCache[id]['start_time']
    var end_time = document.getElementById('end_time');
    end_time.innerText = "End Time \n\t" + eventCache[id]['end_time'];
    fetchVenueDetails(eventCache[id]['venue_id']);
}


</script>


</head>


<body onload="helloPressed();">
<!--<button dojoType="Button" widgetId="helloButton" onclick='helloPressed();'>Hello World!</button>-->


<br>

<h2>Events</h2>

<div id="result_container">
    <form>

        <select id="event_names" onchange="showNameForEvent(this.options[this.selectedIndex].value);">
            <option>Select One</option>
        </select>
        <br><br>

        <div id="right" style="text-align:right;float:right;">
            <div id="start_date"></div>
            <div id="end_date"></div>
            <br>
            <div id="start_time"></div>
            <div id="end_time"></div>
			<br>
            <div id="venue_details"></div>
        </div>
        <div id="left">
            <textarea id="description" rows="25" cols="90"></textarea>
        </div>
    </form>
    None.
</div>
</body>
</html>